package com.wisdytech.linkdcs.partLife.service.impl;

import com.wisdytech.linkdcs.partLife.model.KeyPart;
import com.wisdytech.linkdcs.partLife.model.ModelPart;
import com.wisdytech.linkdcs.partLife.model.Part;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.constants.CommonConstants;
import com.wisdytech.common.exception.ServiceException;
import com.wisdytech.common.handler.BaseService;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.linkdcs.partLife.dao.IKeyPartDao;
import com.wisdytech.linkdcs.partLife.dao.IModelPartDao;
import com.wisdytech.linkdcs.partLife.dao.IPartDao;
import com.wisdytech.linkdcs.partLife.service.IPartService;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  备件信息服务实现类
 * </p>
 *
 * @author chenbin
 * @since 2018-06-05
 */
@Service("iPartService")
@Transactional
public class PartServiceImpl extends BaseService<IPartDao, Part> implements IPartService {

    /**
     * 注入备件信息管理dao
     */
	@Resource
	private IPartDao iPartDao;
    /**
     * 注入设备备件配置dao
     */
	@Resource
	private IModelPartDao iModelPartDao;
    /**
     * 注入关键备件配置dao
     */
	@Resource
	private IKeyPartDao iKeyPartDao;
	
	@Override
	public PageInfo<Part> listPartByFilter(String partCode, String partName, String modelCode, String partType,
			String quickWearFlag, String keyPartFlag,String partSpecification, Filter filter) {
        EntityWrapper<Part> ewPart = new EntityWrapper<>();
        if (StringUtils.isNotBlank(partCode)) {
        	ewPart.like("PART_CODE", partCode);
        }
        if (StringUtils.isNotBlank(partName)) {
        	ewPart.like("PART_NAME", partName);
        }
        if (StringUtils.isNotBlank(modelCode)) {
        	ewPart.eq("MODEL_CODE", modelCode);
        }
        if (StringUtils.isNotBlank(partType)) {
        	ewPart.eq("PART_TYPE", partType);
        }
        if (StringUtils.isNotBlank(quickWearFlag)) {
        	ewPart.eq("QUICK_WEAR_FLAG", quickWearFlag);
        }
        if (StringUtils.isNotBlank(keyPartFlag)) {
        	ewPart.eq("KEY_PART_FLAG", keyPartFlag);
        }
        if (StringUtils.isNotBlank(partSpecification)) {
        	ewPart.eq("PART_SPECIFICATION", partSpecification);
        }
        ewPart.orderBy("create_date", false);
        List<Part> list = super.beforeList(IPartDao.class, filter)
                .selectList(ewPart.eq("del_flag", CommonConstants.DEL_FLAG));
        PageInfo<Part> partPage = new PageInfo<>(list);
		return partPage;
	}

	@Override
	public void addPart(Part part, String loginName) {
		EntityWrapper<Part> ew = new EntityWrapper<>();
        //验证备件编码的唯一性
        List<Part> partList = new ArrayList<>();
        partList = iPartDao.selectPage(new Page<Part>(1, 1), ew.eq("PART_CODE", part.getPartCode()).eq("del_flag", CommonConstants.DEL_FLAG));
        if (!partList.isEmpty()) {
        	throw new ServiceException("该备件编码被使用");
        }
        part.setPkId(BaseUtil.getUUID());
        part.setDelFlag(CommonConstants.DEL_FLAG);
        part.setCreateUser(loginName);
        part.setCreateDate(new Date());
        int partCount = iPartDao.insert(part);
        if (partCount <= 0) {
            throw new ServiceException("新增失败");
        }
        
		
	}

	@Override
	public void updatePart(Part part, String loginName) {
		EntityWrapper<Part> ew = new EntityWrapper<>();
        //验证备件编码的唯一性--修改功能
		List<Part> partList;
		partList = iPartDao.selectPage(new Page<Part>(1, 1),ew.eq("PART_CODE", part.getPartCode()).ne("pk_id", part.getPkId()));
        if (partList.size() > 0){
        	throw new ServiceException("该备件编码被使用");
        }
        //获取即将更改的备件
        Part partNow = iPartDao.selectById(part.getPkId());
        EntityWrapper<KeyPart> ewk = new EntityWrapper<>();
        ewk.eq("PART_ID", part.getPkId());
        List<KeyPart> listKey = iKeyPartDao.selectList(ewk);
        //如果原备件为关键备件，而且被添加，且即将由关键备件改为非关键备件
        if(partNow.getKeyPartFlag()==0 && (!listKey.isEmpty()) && part.getKeyPartFlag()==1) {
        	throw new ServiceException("该备件已添加为关键备件");
        }
        part.setUpdateDate(new Date());
        part.setUpdateUser(loginName);
        int operationCount = iPartDao.updateById(part);
        if (operationCount <= 0) {
            throw new ServiceException("修改失败");
        }
	}

	@Override
	public void delPartById(String ids) {
        List<String> idList = new ArrayList<String>();
        String[] id = ids.split(",");
        for (int i = 0; i < id.length; i++) {
            idList.add(id[i]);
        }
        for(String pkId : id) {
            //通过设备的id查询所有设备配置
            EntityWrapper<ModelPart> ew = new EntityWrapper<>();
            ew.eq("PART_ID", pkId);
            int num = this.iModelPartDao.selectCount(ew);
            if(num>0) {
            	throw new ServiceException("所选记录中存在设备关联的备件");
            }
        }
        int partCount = iPartDao.deleteBatchIds(idList);
        if (partCount <= 0) {
            throw new ServiceException("删除失败");
        }
		
	}

	@Override
	public Part getPartById(String pkId) {
		return iPartDao.selectById(pkId);
	}

	@Override
	public List<Part> listKeyPartAll(String search) {
        EntityWrapper<Part> ew = new EntityWrapper<>();
        if (StringUtils.isNotBlank(search)) {
            ew.like("PART_CODE", search);
        }
        ew.eq("KEY_PART_FLAG", 0);
        List<Part> partList = iPartDao.selectList(ew);
        return partList;
	}

	@Override
	public Part getPartByPartCode(String partCode) {
		Part part = new Part();
		part.setPartCode(partCode);
		return iPartDao.selectOne(part);
	}

	@Override
	public List<Part> listModelPartAll(String search, String modelCode,Filter filter) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("partCode", search);
        queryMap.put("modelCode", modelCode);
        List<Part> lists =
                super.beforeList(IPartDao.class, filter).listModelPartAll(queryMap);
        return lists;
	}

	@Override
	public List<Part> listlPartAllByPartCode(String search, Filter filter) {
        EntityWrapper<Part> ewPart = new EntityWrapper<>();
        if (StringUtils.isNotBlank(search)) {
        	ewPart.like("PART_CODE", search);
        }
        List<Part> list = super.beforeList(IPartDao.class, filter)
                .selectList(ewPart.eq("del_flag", CommonConstants.DEL_FLAG));
        return list;
	}

}
